package com.heo.servlet;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.Timer;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.heo.pub.SysConfig;

/**
 * Servlet implementation class Log4jLoader
 */

public class ConfigLoader extends HttpServlet
{
  private static Logger log = Logger.getLogger(ConfigLoader.class);
  private static final long serialVersionUID = 1L;
  private static final String CONTENT_TYPE = "text/html; charset=UTF-8";

  @Override
  public void destroy()
  {

  }

  /**
   * @see HttpServlet#HttpServlet()
   */
  public ConfigLoader()
  {
    super();
  }

  /**
   * @see Servlet#init(ServletConfig)
   */
  @Override
  public void init(ServletConfig config) throws ServletException
  {
    try
    {
      super.init(config);
      // 在serlvet里使用spring的注入
      WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());

      // 读取配置文件信息
      String file = config.getInitParameter("resource");
      
      InputStream is = getServletContext().getResourceAsStream(file);
      Properties props = new Properties();
      props.load(is);


      log.info("AppConfig has Loaded......");
    }
    catch (Exception e)
    {
      log.error(e.getMessage(), e);
      log.error("AppConfig load error......");
    }
  }

  /**
   * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    response.setContentType(CONTENT_TYPE);
  }

}
